package com.icecloud.cloud.filestore.conf.oauth;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;

/**
 * @author penghaozhong E-mail:penghaozhong@163.com
 * @version 2016年11月10日 下午4:10:04
 * 
 */
@Configuration
public class OAuth2ServerConfig {

	/**
	 * 资源服务器
	 * 
	 * @author penghaozhong
	 *
	 */
	@Configuration
	@EnableResourceServer
	protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

		@Override
		public void configure(ResourceServerSecurityConfigurer resources) {
			resources.resourceId("app").stateless(false);
		}

		@Override
		public void configure(HttpSecurity http) throws Exception {
			http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED).and().requestMatchers()
					.antMatchers("/file/**").and().authorizeRequests().antMatchers("/file/**")
					.access("#oauth2.hasScope('trust')");
		}

	}

	/**
	 * oauth2 服务端
	 * 
	 * @author penghaozhong
	 *
	 */
	@Configuration
	@EnableAuthorizationServer
	@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)
	protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {

		@Autowired
		private DataSource dataSource;
		
		@Autowired
		private AuthenticationManager authenticationManager;
		@Autowired
		private TokenStore tokenStore;
//		@Autowired
//		RedisConnectionFactory redisConnectionFactory;
		@Autowired
		private JdbcClientDetailsService jdbcClientDetailsService;
		

		@Override
		public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
			clients.withClientDetails(jdbcClientDetailsService);
		}

		@Override
		public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
			endpoints.tokenStore(tokenStore).authenticationManager(authenticationManager);
		}
		
//		
//	  @Bean  
//	   public RedisConnectionFactory redisConnectionFactory(){  
//	        JedisConnectionFactory redisConnectionFactory=new JedisConnectionFactory();  
//	        redisConnectionFactory.setHostName("localhost");  
//	        redisConnectionFactory.setUsePool(true);  
//	        return redisConnectionFactory;  
//	    }  

		@Bean
		public JdbcClientDetailsService jdbcClientDetailsService() {
			JdbcClientDetailsService  jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);
			//jdbcClientDetailsService.setPasswordEncoder(new StandardPasswordEncoder(new StringBuffer("ice-cloud")));
			return  jdbcClientDetailsService;
			//			return new RedisTokenStore(redisConnectionFactory);
		}
		
		@Bean
		public TokenStore tokenStore() {
			return new JdbcTokenStore(dataSource);
//			return new RedisTokenStore(redisConnectionFactory);
		}
		
	

	}
}
